import java.util.Stack;

public class Test {
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        if (s == null) {
            return false;
        }
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if (stack.empty()) {
                    return false;
                }
                char ch2 = stack.peek();
                if (ch2 == '(' && ch == ')' || ch2 == '{'
                && ch == '}' || ch2 == '[' && ch == ']'
                ) {
                    stack.pop();
                }else {
                    return  false;
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;
    }

}
